<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Jepson的博客系统</title>
    <!-- bass CSS-->
    <link rel="stylesheet" href="./css/base.css">
    <!-- Bootstrap Core CSS -->
    <link href="../assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <!-- Theme CSS -->
    <link href="../assets/css/sb-admin.css" rel="stylesheet">
    <!-- Custom Fonts -->
    <link href="../assets/vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
    <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div id="wrapper">
    <!-- Navigation -->
    <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="index.html">Jepson</a>
        </div>
        <!-- Top Menu Items -->
        <ul class="nav navbar-right top-nav">
            <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-envelope"></i> <b
                        class="caret"></b></a>
                <ul class="dropdown-menu message-dropdown">
                    <li class="message-preview">
                        <a href="#">
                            <div class="media">
                                <span class="pull-left"><img class="media-object" src="http://placehold.it/50x50"
                                                             alt=""></span>
                                <div class="media-body">
                                    <h5 class="media-heading"><strong>John Smith</strong></h5>
                                    <p class="small text-muted"><i class="fa fa-clock-o"></i> Yesterday at 4:32 PM</p>
                                    <p>Lorem ipsum dolor sit amet, consectetur...</p>
                                </div>
                            </div>
                        </a>
                    </li>
                    <li class="message-preview">
                        <a href="#">
                            <div class="media">
                                <span class="pull-left"><img class="media-object" src="http://placehold.it/50x50"
                                                             alt=""></span>
                                <div class="media-body">
                                    <h5 class="media-heading"><strong>John Smith</strong></h5>
                                    <p class="small text-muted"><i class="fa fa-clock-o"></i> Yesterday at 4:32 PM</p>
                                    <p>Lorem ipsum dolor sit amet, consectetur...</p>
                                </div>
                            </div>
                        </a>
                    </li>
                    <li class="message-preview">
                        <a href="#">
                            <div class="media">
                                <span class="pull-left"><img class="media-object" src="http://placehold.it/50x50"
                                                             alt=""></span>
                                <div class="media-body">
                                    <h5 class="media-heading"><strong>John Smith</strong></h5>
                                    <p class="small text-muted"><i class="fa fa-clock-o"></i> Yesterday at 4:32 PM</p>
                                    <p>Lorem ipsum dolor sit amet, consectetur...</p>
                                </div>
                            </div>
                        </a>
                    </li>
                    <li class="message-footer">
                        <a href="#">Read All New Messages</a>
                    </li>
                </ul>
            </li>
            <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-bell"></i> <b
                        class="caret"></b></a>
                <ul class="dropdown-menu alert-dropdown">
                    <li><a href="#">Alert Name <span class="label label-default">Alert Badge</span></a></li>
                    <li><a href="#">Alert Name <span class="label label-primary">Alert Badge</span></a></li>
                    <li><a href="#">Alert Name <span class="label label-success">Alert Badge</span></a></li>
                    <li><a href="#">Alert Name <span class="label label-info">Alert Badge</span></a></li>
                    <li><a href="#">Alert Name <span class="label label-warning">Alert Badge</span></a></li>
                    <li><a href="#">Alert Name <span class="label label-danger">Alert Badge</span></a></li>
                    <li class="divider"></li>
                    <li><a href="#">View All</a></li>
                </ul>
            </li>
            <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-user"></i> Jepson <b
                        class="caret"></b></a>
                <ul class="dropdown-menu">
                    <li><a href="#"><i class="fa fa-fw fa-user"></i> Profile</a></li>
                    <li><a href="#"><i class="fa fa-fw fa-envelope"></i> Inbox</a></li>
                    <li><a href="#"><i class="fa fa-fw fa-gear"></i> Settings</a></li>
                    <li class="divider"></li>
                    <li><a href="#"><i class="fa fa-fw fa-power-off"></i> Log Out</a></li>
                </ul>
            </li>
        </ul>
        <!-- Sidebar Menu Items - These collapse to the responsive navigation menu on small screens -->
        <div class="collapse navbar-collapse navbar-ex1-collapse">
            <ul class="nav navbar-nav side-nav">
                <li><a href="index.html"><i class="fa fa-fw fa-dashboard"></i> 帖子管理 </a></li>
                <li class="active"><a href="person.html"><i class="fa fa-fw fa-table"></i> 用户管理</a></li>
                <!--<li><a href="bootstrap-forms.html"><i class="fa fa-fw fa-edit"></i> Forms</a></li>-->
                <!--<li><a href="bootstrap-elements.html"><i class="fa fa-fw fa-desktop"></i> Bootstrap Elements</a></li>-->
                <!--<li><a href="bootstrap-grid.html"><i class="fa fa-fw fa-wrench"></i> Bootstrap Grid</a></li>-->
                <li>
                    <a href="javascript:;" data-toggle="collapse" data-target="#demo"><i
                            class="fa fa-fw fa-arrows-v"></i> Dropdown <i class="fa fa-fw fa-caret-down"></i></a>
                    <ul id="demo" class="collapse">
                        <li><a href="#">Dropdown Item</a></li>
                        <li><a href="#">Dropdown Item</a></li>
                    </ul>
                </li>
                <!--<li><a href="blank-page.html"><i class="fa fa-fw fa-file"></i> Blank Page</a></li>-->
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </nav>
    <div id="page-wrapper" style="min-height: 100vh;">
        <div class="container-fluid">
            <!-- Page Heading -->
            <h1 class="page-header">用户管理</h1>
            <div class="row">
                <div class="col-lg-12">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            <h3 class="panel-title"><i class="fa fa-money fa-fw"></i> 用户管理 </h3>
                        </div>
                        <div class="panel-body">
                            <div class="table-responsive">
                                <button id='btnAdd' class='btn btn-default' type='button'>添加新用户</button>

                                <!--<div class=input-group>-->
                                      <!--<input type="text" id='txtSearch' class='form-control'>-->
                                      <!--<span class='input-group-btn'>-->
                                          <!--<button id='btnSearch' class='btn btn-default' type='button'>Go!</button>-->
                                      <!--</span>-->
                                <!--</div>-->
                                <table class="table table-bordered table-hover table-striped">
                                    <thead>
                                    <tr>
                                        <th>序号</th>
                                        <th>昵称</th>
                                        <th>姓名</th>
                                        <th>密码</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody id='tbody'></tbody>
                                    <script id='tempTbody' type='text/html'>
                                        {{ each data as value i }}
                                        <tr>
                                            <td>{{value.id}}</td>
                                            <td>{{value.nickname}}</td>
                                            <td>{{value.username}}</td>
                                            <td>{{value.password}}</td>
                                            <td data-id='{{value.id}}'>
                                                <button type="button" class="btn btn-primary">编辑</button>
                                                <button type="button" class="btn btn-danger">删除</button>
                                            </td>
                                        </tr>
                                        {{ /each }}
                                    </script>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- /.row -->
        </div>
        <!-- /.container-fluid -->
    </div>
    <!-- /#page-wrapper -->
</div>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span
                        class="sr-only">关闭</span></button>
                <h4 class="modal-title" id="myModalLabel">添加</h4>
            </div>
            <div class="modal-body">
                <form id='form' role='form'>
                    <input type="hidden" id='id' name='id'>
                    <div class='form-group'>
                        <lable for='nickname'>用户昵称：</lable>
                        <input type="text" class='form-control' id='nickname' placeholder='Edit nickname' name='nickname'>
                    </div>
                    <div class='form-group'>
                        <lable for='nickname'>用户名：</lable>
                        <input type="text" class='form-control' id='username' placeholder='Edit username' name='username'>
                    </div>
                    <div class='form-group'>
                        <lable for='nickname'>密码：</lable>
                        <input type="text" class='form-control' id='password' placeholder='Edit password' name='password'>
                    </div>
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" id='btnSave'>保存</button>
            </div>
        </div>
    </div>
</div>
<!-- /#wrapper -->
<!-- jQuery -->
<script src="../assets/vendor/jquery/jquery.min.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="../assets/vendor/bootstrap/js/bootstrap.min.js"></script>
<!-- art-template -->
<script src='../assets/vendor/template/template.js'></script>
<!-- layer -->
<script src='../assets/vendor/layer/layer.js'></script>
<!-- 加载 ueditor -->
<script charset="utf-8" src="../assets/vendor/ueditor/ueditor.config.js"></script>
<script charset="utf-8" src="../assets/vendor/ueditor/ueditor.all.min.js"></script>
<!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
<!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
<script charset="utf-8" src="../assets/vendor/ueditor/lang/zh-cn/zh-cn.js"></script>
</body>
<script>
    (function () {
        /* 页面参数 */
        var baseUrl = 'http://127.0.0.1:8888';

        /* 页面主函数 集中管理页面中的全部功能 */
        (function () {
            /* 获取所有用户 */
            loadAllUsers();  // 公共方法

            /* 添加修改用户 */
            editUser();

            /* 删除用户 */
            deleteUser();
//
//            /* 搜索功能 */
//            searchUsers();

        })();

        /* 获取所有用户 */
        function loadAllUsers() {
            /* 获取数据 渲染 */
            $.getJSON(baseUrl + '/admin/users', function (data) {
                /* 判断权限 */
                if (data && data.code === -1) {
                    location.href = '../sign.html';
                    return;
                }
                /* 渲染全部 */
                $('#tbody').html(template('tempTbody', {'data': data}));
            });
        }

        /* 添加修改用户 */
        function editUser() {
            /* 获取弹层 */
            var $myModel = $('#myModal');
            var flag = 1; // 标记 1 添加, 2 修改

            /* 添加按钮 */
            $('#btnAdd').click(function () {
                // 修改标题
                $("#myModalLabel").text("添加用户");
                /* 显示弹层 */
                $myModel.modal("show");
                /* 标记添加 */
                flag = 1;
            });

            /* 修改按钮 */
            $('#tbody').on('click', '.btn-primary', function () {
                // 获取 id
                var id = $(this).parent('td').data('id');
                /* 获取内容 */
                $.getJSON(baseUrl + '/admin/users/' + id, function (data) {
                    if (data && data.code !== -1) {
                        // 修改标题
                        $("#myModalLabel").text("编辑用户");
                        /* 设置 id */
                        $('#id').val(id);
                        /* 设置 nickname */
                        $('#nickname').val(data.nickname);
                        /* 设置 nickname */
                        $('#username').val(data.username);
                        /* 设置 nickname */
                        $('#password').val(data.password);
                        /* 显示弹层 */
                        $myModel.modal("show");
                        /* 标记修改 */
                        flag = 2;
                    }
                });
            });

            /* 保存按钮 */
            $myModel.on('click', '.btn-primary', function () {
                /* 表单序列化，卧槽这个方法超级牛逼哇！*/
                var data = $("#form").serialize();
                // id=&nickname=12321&username=12312&password=12321
                /* 公共回调函数 */
                var callback = function( data ) {
                    if (data && data.code === 1) {
                        // 提示层
                        layer.msg(data.msg);
                        // 关闭modal
                        $("#myModal").modal("hide");
                        // 重新加载全部
                        loadAllUsers();
                    } else {
                        layer.msg('保存失败，请稍候重试');
                    }
                };
                // 1 添加操作
                if (flag === 1) {
                    $.post(baseUrl + '/admin/users/create', data, function (data) {
                        callback( data );
                    }, 'json');
                }
                // 2 修改操作
                else if (flag === 2) {
                    var id = data.split( '&' )[0].split('=')[1];
                    $.post(baseUrl + '/admin/users/update/'+ id, data, function (data) {
                        callback( data );
                    }, 'json');
                }
            });
        }

        /* 删除用户 */
        function deleteUser() {
            /* 点击事件监听 */
            $('#tbody').on('click', '.btn-danger', function () {
                // 获取 id
                var id = $(this).parent('td').data('id');
                //询问框
                layer.msg('确认删除么', {
                    time: 0, //不自动关闭
                    btn: ['确认', '取消'],
                    yes: function (index) {
                        layer.close(index);
                        $.getJSON(baseUrl + '/admin/users/delete/' + id, function (data) {
                            /* 判断权限 */
                            if (data && data.code === -1) {
                                location.href = '../sign.html';
                                return;
                            }
                            layer.msg(data.msg);
                            /* 删除完了重新渲染 */
                            loadAllUsers();
                        });
                    }
                });
            });
        }

        /* 搜索用户 */
        function searchUsers() {
            $('#btnSearch').on('click', function () {
                var value = $('#txtSearch').val();
                /* 有值就搜索，没值就渲染 */
                if (value) {
                    $.getJSON(baseUrl + '/admin/posts/search', {wd: value}, function (data) {
                        /* 判断权限 */
                        if (data && data.code === -1) {
                            location.href = '../sign.html';
                            return;
                        }
                        /* 渲染全部搜索 */
                        $('#tbody').html(template('tempTbody', {'data': data}));
                    });
                } else {
                    /* 加载全部数据 */
                    loadAllPosts();
                }
            });
        }
    })();
</script>
</html>
